package com.lwz.startdemo.recyclerview

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import com.lwz.startdemo.R

class MsgAdapter (val msgList:List<Msg>):RecyclerView.Adapter<MsgViewHolder>(){

    override fun getItemViewType(position: Int): Int {
        val msg = msgList[position]
        return msg.type
    }

//    inner class LeftViewHolder(view:View):RecyclerView.ViewHolder(view){
//        val leftMsg = view.findViewById<TextView>(R.id.tvContent)
//    }
//
//    inner class RightViewHolder(view:View):RecyclerView.ViewHolder(view){
//        val rightMsg = view.findViewById<TextView>(R.id.tvContent)
//    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = if (viewType==Msg.TYPE_RECEIVED) {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.msg_left_item,parent,false)
        MsgViewHolder.LeftViewHolder(view)
    }else{
        val view = LayoutInflater.from(parent.context).inflate(R.layout.msg_right_item,parent,false)
        MsgViewHolder.RightViewHolder(view)
    }

    override fun getItemCount(): Int = msgList.size

    override fun onBindViewHolder(holder: MsgViewHolder, position: Int) {
        val fruit = msgList[position]
        when(holder){
            is MsgViewHolder.LeftViewHolder -> holder.leftMsg.text = fruit.content
            is MsgViewHolder.RightViewHolder -> holder.rightMsg.text = fruit.content
            else -> Toast.makeText(holder.itemView.context,"error",Toast.LENGTH_SHORT).show()
        }
    }

}